package wareHouse;

import logIn.MysqlConnectionTool;
import manage.selectLeft;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 管理员出库功能实现
 * @Author Jian Chen long
 * @Date 2021/6/18 14:49
 */
public class outHouse extends JFrame {
    private JLabel label = new JLabel("出库系统");
    private JPanel panel = new JPanel();

    private JLabel model = new JLabel("型号");
    private JTextField modelText = new JTextField(16);

    private JLabel brand = new JLabel("品牌");
    private JTextField brandText = new JTextField(16);

    private JLabel count = new JLabel("数量");
    private JTextField countText = new JTextField(16);
    private JPanel panel1 = new JPanel(new GridLayout(3,1,0,0));

    private JPanel panel2 = new JPanel(new GridLayout(1,1,30,0));
    private JButton yesBtn = new JButton("出库");
    private JButton leftBtn = new JButton("库存");
    private JButton noBtn = new JButton("取消");

    public outHouse(){
        panel.add(label);
        panel1.add(model);
        panel1.add(modelText);
        panel1.add(brand);
        panel1.add(brandText);
        panel1.add(count);
        panel1.add(countText);
        panel2.add(yesBtn);
        panel2.add(leftBtn);
        panel2.add(noBtn);
        panel.add(panel1,BorderLayout.NORTH);
        panel.add(panel2,BorderLayout.SOUTH);
        this.add(panel);
        this.setTitle("出库系统");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setSize(420,180);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        /**
         * 出库按钮功能实现
         */
        yesBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String mode = modelText.getText();
                String brand = brandText.getText();
                String outConut = countText.getText();

                Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                try{
                    //获取连接
                    conn = MysqlConnectionTool.getConnection();
                    // 判断信息输入是否完整
                    if (mode.length() == 0 || brand.length() == 0 || outConut.length() == 0){
                        JOptionPane.showMessageDialog(null,"请输入完整信息");
                        modelText.setText(null);
                        brandText.setText(null);
                        countText.setText(null);
                        return;
                    }
                    String sqlCount = "select * from inhouse where inname = ? and inbrand = ?";
                    ps = conn.prepareStatement(sqlCount);
                    ps.setString(1,mode);
                    ps.setString(2,brand);
                    rs = ps.executeQuery();
                    // 出库的数量
                    int count = Integer.parseInt(outConut);
                    while (rs.next()){
                        // 原有仓库的物资数量
                        int inCount = Integer.parseInt(rs.getString("incount"));
                        // 判断出库数量是否正确,是否<=0或者>仓库中的数量
                        if (count <= 0 || count > inCount){
                            JOptionPane.showMessageDialog(null,"数量错误");
                            countText.setText(null);
                            return;
                        }else {
                            // 剩余数量
                            int leftCount = inCount - count;
                            /**
                             * 数据插入出库表
                             */
                            String insertOutHouseSql = "insert into outhouse(outname,outbrand,outprice,outcount,outleft) values(?,?,?,?,?)";
                            ps = conn.prepareStatement(insertOutHouseSql);
                            ps.setString(1,mode);
                            ps.setString(2,brand);
                            // rs.getString("inprice") 品牌价格
                            ps.setString(3,rs.getString("inprice"));
                            ps.setString(4,outConut);
                            ps.setString(5,String.valueOf(leftCount));
                            ps.executeUpdate();

                            /**
                             * 入库表数量更新
                             */
                            String updateCountSql = "update inhouse set incount = " + leftCount + " where inname = ? and inbrand = ?";
                            ps = conn.prepareStatement(updateCountSql);
                            ps.setString(1,mode);
                            ps.setString(2,brand);
                            int flag = ps.executeUpdate();
                            if (flag == 1){
                                if (leftCount == 0){
                                    String dropCountSql = "delete from inhouse where inname = ? and inbrand = ?";
                                    ps = conn.prepareStatement(dropCountSql);
                                    ps.setString(1,mode);
                                    ps.setString(2,brand);
                                    int dropFlag = ps.executeUpdate();
                                    // 如果仓库内物资剩余数量为0的话,删除数据
                                    if (dropFlag == 1){
                                        JOptionPane.showMessageDialog(null,"出库成功");
                                        modelText.setText(null);
                                        brandText.setText(null);
                                        countText.setText(null);
                                        return;
                                    }
                                }
                                return;
                            }
                        }

                    }
                    // 程序如果执行到这里说明,仓库里没有这个物资,询问是否先入库
                    JOptionPane.showMessageDialog(null,"没有这个物资,请先入库");
                    modelText.setText(null);
                    brandText.setText(null);
                    countText.setText(null);
                    return;
                }catch (Exception E){
                   JOptionPane.showMessageDialog(null,"输入正确的数量");
                    countText.setText(null);
                   return;
                }finally {
                    MysqlConnectionTool.close(conn,ps,rs);
                }

            }
        });

        /**
         * 库存按钮功能实现
         */
        leftBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new selectLeft().setVisible(true);
            }
        });



        /**
         * 取消按钮功能实现
         */
        noBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"系统退出");
                System.exit(0);
            }
        });
    }
}
